import stackless
import time
import pygame
from pygame.locals import *

class Input(object):
    """Base class of all input devices.

    Takes a channel for x and z in a physics.SpaceShip()
    and sends the values for x and z directions between
    -1.0 and 1.0"""
    def __init__(self, x, z, quit):
        self.x = x
        self.z = z
        self.quit = quit

    def process_events(self):
        pass

    def fire(self):
        pass

    def rotate(self):
        self.x.send(self.axes[1])
        self.z.send(self.axes[0])

    def thrust(self, thrust):
        pass

class Joystick(Input):
    def __init__(self, id, x, z, quit):
        pygame.joystick.init()
        self.joystick = pygame.joystick.Joystick(id)
        self.joystick.init()
        self.axes = [0, 0, 0]
        self.button = [self.fire]
        Input.__init__(self, x, z, quit)

    def process_events(self):
        #events = pygame.event.get(JOYAXISMOTION)
        #events = dict([(e.axis, e.value) for e in events])
        #for axis in events:
        #    print "#####", events[axis], "######"
        #    if not 0.02 > events[axis] > -0.02:
        #        self.axes[axis] = events[axis]
        #    else:
        #        self.axes[axis] = 0
        self.axes[0] = -self.joystick.get_axis(0)
        self.axes[1] = self.joystick.get_axis(1)
        self.rotate()
        for key_down in pygame.event.get(KEYDOWN):
            if key_down.key == K_ESCAPE:
                self.quit()
                #time.sleep(0.1)
                #raise SystemExit
        pygame.event.get()
#        pygame.event.pump()
        stackless.schedule()

class Keyboard(Input):
    #keys = {'up': K_UP, 'down': K_DOWN, 'left': K_LEFT, 'right': K_RIGHT,
    #        'inc_thrust': K_EQUALS, 'dec_thrust': K_MINUS, 'fire': K_SPACE}
    #keys = {K_UP: self.rotate, K_DOWN: self.rotate, K_LEFT: self.rotate,
    #        K_EQUALS: self.thrust, K_MINUS: self.thrust, K_SPACE: self.fire}
    #def __init__(self):
    #def set_keys(self, dict):
    axes = [0,0,0]

    def process_events(self):
        for event in pygame.event.get(KEYDOWN):
            print pygame.key.name(event.key)
            if event.key == K_UP:
                self.axes[1] = -1
            elif event.key == K_DOWN:
                #print "down"
                self.axes[1] = 1
            elif event.key == K_ESCAPE:
                pygame.quit()
                time.sleep(0.1)
                print "still running"
                self.quit()
                #raise SystemExit
        for event in pygame.event.get(KEYUP):
            if event.key in (K_UP, K_DOWN):
                self.axes[1] = 0
        self.rotate()
        stackless.schedule()



if __name__ == "__main__":
    import time
    def receive(channel, axis):
        while 1:
            print channel.receive(), axis
    def run(j):
        for i in range(10):
            j.process_events()
            time.sleep(0.2)
            stackless.schedule()
    pygame.init()
    x=stackless.channel()
    z=stackless.channel()
    j = Joystick(0, x, z)
    stackless.tasklet(receive)(x, "x")
    stackless.tasklet(receive)(z, "z")
    stackless.tasklet(run)(j)
    print "move that joystick!!"
    stackless.run()
    #for i in xrange(10):
    #    time.sleep(0.2)
    print "event.get", pygame.event.get(pygame.JOYBUTTONDOWN)
    #j.process_events()#pygame.event.get(pygame.JOYAXISMOTION))
    print j.axes
